package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.sun.org.apache.regexp.internal.RE;
import com.sun.org.apache.regexp.internal.recompile;




import pojo.Customer;
import util.DBUtil;
import util.DBUtilJNDI;
import dao.CustomerDao;

@Repository(value = "customerDaoImpl")
public class CustomerDaoImpl implements CustomerDao{
	@Resource(name="dataSource1")
	private DataSource ds;
	

	public void setDs(DataSource ds) {
		this.ds = ds;
	}

	@Override
	public int addCustomer(Customer c) {
		String sql="insert into customer(id,name,age,sal,birth,sex)values(?,?,?,?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		ResultSet rs = null;
		int m = 0;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, c.getId());
			pst.setString(2, c.getName());
			pst.setInt(3, c.getAge());
			pst.setDouble(4, c.getSal());
			pst.setDate(5, new java.sql.Date(c.getBirth().getTime()));
			pst.setBoolean(6, c.isSex());
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return m;
	}

	@Override
	public int deleteCustomerById(String id) {
		String sql="delete customer where id =?";
		Connection conn=null;
		PreparedStatement pst =null;
		int m=0;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, id);
			m=pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return m;
	}
		
	

	@Override
	public List<Customer> findAllCustomer() {
		List<Customer> cl = new ArrayList<Customer>();
		String sql="select * from customer order by id";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Customer c =null;
		int m =0;
		try {
			conn=DBUtil.createConnection();
			pst=conn.prepareStatement(sql);
			rs=pst.executeQuery();
			while(rs.next()){
				c= new Customer();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setAge(rs.getInt("age"));
				c.setSal(rs.getDouble("sal"));
				c.setBirth(new Date(rs.getDate("birth").getTime()));
				c.setSex(rs.getBoolean("sex"));
				cl.add(c);
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		
		return cl;
	}
	

	@Override
	public int updateCustomer(Customer c) {
		String sql="update customer set name=?,age=?,sal=?,birth=?,sex=? where id =?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		StringBuilder sb= new StringBuilder();
		int m=0;
		try {
			conn=ds.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, c.getName());
			pst.setInt(2, c.getAge());
			pst.setDouble(3, c.getSal());
			pst.setDate(4, new java.sql.Date(c.getBirth().getTime()));
			pst.setBoolean(5, c.isSex());
			pst.setString(6, c.getId());
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			try {
				if(rs!=null)rs.close();
				if(pst!=null)pst.close();
				if(conn!=null)conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(m);
		return m;
	}

}
